package com.guigu.dh.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.dh.api.service.ApiDepartmentService;
import com.guigu.dh.api.service.ApiScheduleService;
import com.guigu.dh.common.result.Result;
import com.guigu.dh.common.utils.MD5;
import com.guigu.dh.pojo.hosp.Department;
import com.guigu.dh.pojo.hosp.Hospital;
import com.guigu.dh.pojo.hosp.HospitalSet;
import com.guigu.dh.api.service.ApiHospitalService;
import com.guigu.dh.pojo.hosp.Schedule;
import com.guigu.dh.service.HospitalSetService;
import com.guigu.dh.vo.hosp.CheckSignKeyVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@Api(tags = "医院管理接口")
@RestController
@RequestMapping("/api/hospital/")
@Slf4j
public class ApiController {

    @Autowired
    private ApiHospitalService apiHospitalService;
    @Autowired
    private HospitalSetService setService;
    @Autowired
    private ApiDepartmentService apiDepartmentService;
    @Autowired
    private ApiScheduleService apiScheduleService;

    // 保存医院信息的方法
    @PostMapping("/saveHospital")
    public Result saveHospital(@RequestBody Hospital hospital){

        HospitalSet hospitalSet = setService.getOne(
                new QueryWrapper<HospitalSet>()
                        .eq("hoscode",hospital.getHoscode()));

        if (hospitalSet != null && MD5.encrypt(hospitalSet.getSignKey()).equals(hospital.getSignKey())) {

            Boolean result = apiHospitalService.saveHospital(hospital);
            if (!result) {
                return Result.fail();
            }

            return Result.ok();
        }
        log.error("医院不存在或无效的密匙! 无法应用该接口! => {}",hospital);
        return Result.fail();
    }


    // 验证签名密匙
    @PostMapping("/checkSignKey")
    public Result checkSignKey (@RequestBody HospitalSet hospitalSet){
        //取hospital对象中的医院编码和签名密匙
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();

        // 根据hoscode查询医院信息是否可用
        HospitalSet hoscode1 = setService.getOne(
                new QueryWrapper<HospitalSet>()
                        .eq("hoscode",hoscode));

        // 判断是否可用
        if (hoscode1 != null) {
            String signKey1 = MD5.encrypt(hoscode1.getSignKey());
            // 判断签名密匙是否正确
            if (signKey1.equals(signKey)) {

                return Result.ok();
            } else {
                return Result.fail();
            }
        } else {
            return Result.fail();
        }
    }

    // 查询显示医院的接口
    @PostMapping("/show")
    public Result show(@RequestBody CheckSignKeyVo checkSignKeyVo){
        return apiHospitalService.show(checkSignKeyVo);
    }

    // 保存科室的接口
    @PostMapping("/saveDepartment")
    public Result saveDepartment(@RequestBody Department department){
        return apiDepartmentService.saveDepartment(department);
    }

    // 分页的查询的方法
    @PostMapping("/pagingDepartment")
    public Result pagingDepartment(@RequestBody Map<String, String> map){
        return apiDepartmentService.pagingDepartment(map);
    }

    @DeleteMapping("delDepartment/{depcode}")
    public Result delDepartmentByDepcode (@PathVariable String depcode) {
        return apiDepartmentService.doDel(depcode);
    }


    // 保存排班的接口
    @PostMapping("/saveSchedule")
    public Result saveSchedule(@RequestBody Schedule schedule){
        return apiScheduleService.saveSchedule(schedule);
    }

    // 分页的查询的方法
    @PostMapping("/pagingSchedule")
    public Result pagingSchedule(@RequestBody Map<String, String> map){
        return apiScheduleService.pagingSchedule(map);
    }

    @DeleteMapping("delSchedule/{depcode}/{hosScheduleId}")
    public Result delSchedule (@PathVariable String depcode,
                                @PathVariable String hosScheduleId) {
        return apiScheduleService.doDel(depcode,hosScheduleId);
    }


}
